package com.newedu.equipmgr.service;

import com.newedu.equipmgr.dao.SysUserDao;
import com.newedu.equipmgr.dao.SysUserDaoImpl;
import com.newedu.equipmgr.entity.SysUser;
import com.newedu.equipmgr.util.MD5Utils;

import java.util.List;


public class SysUserService {
    /**定义一个 数据库操作的成员变量 SysUserDao对象 */
    private final SysUserDao sysUserDao = new SysUserDaoImpl();

    /**
     *  判断用户是否可以登陆系统
     * @param userName 用户名
     * @param pwd 密码
     * @return 该用户是否合法，是否可以登陆系统 true-可以登陆，false-不可以登陆
     */
    public boolean login(String userName,String pwd){
        try {
            /**
             * 业务逻辑判断
             * 1: 查询数据库，返回一个数据库中的映射对象SysUser
             * 2: 拿到数据库中存在的密码
             * 3: md5 加密 此次 用户输入的登陆密码
             * 4: 数据库中的用户密码和此次用户输入的登陆密码做判断
             * */
            SysUser sysUser = sysUserDao.login(userName);
            String pwdFromDB = sysUser.getPwd();
            String md5Pwd = MD5Utils.getMD5(pwd,32);
            if(md5Pwd.equals(pwdFromDB)){
                return true;
            }
        }
        catch (Exception e){
            // 将数据库操作Dao层的数据库异常，转换为Service的运行时异常
            throw new RuntimeException("用户不存在！【"+userName+"】");
        }

        return false;
    }
    public boolean insert(SysUser sysUser){
        boolean insert = false;
        try{
            //在保存用户信息前，对用户的密码进行md5加密操作
            sysUser.setPwd(MD5Utils.getMD5(sysUser.getPwd(),32));
            insert = sysUserDao.insert(sysUser);
        }
        catch (Exception e){
            throw new RuntimeException("用户信息插入失败！");
        }
        return insert;
    }

    public boolean insertUserAndRoleRelationship(SysUser sysUser, int RoleId){
        boolean insert = false;
        try{
            //在保存用户信息前，对用户的密码进行md5加密操作
            sysUser.setPwd(MD5Utils.getMD5(sysUser.getPwd(),32));
            insert = sysUserDao.insertUserAndRoleRelationship(sysUser, RoleId);
        }
        catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("用户信息插入失败！");
        }
        return insert;
    }


    public boolean update(SysUser sysUser,String roleId){
        boolean update = false;
        try{
            //在保存用户信息前，对用户的密码进行md5加密操作
            sysUser.setPwd(MD5Utils.getMD5(sysUser.getPwd(),32));
            update = sysUserDao.update(sysUser,roleId);
        }
        catch (Exception e){
            throw new RuntimeException("用户信息更新失败！");
        }
        return update;
    }

    public boolean delete(int id){
        boolean delete = false;
        try{
            delete = sysUserDao.delete(id);
        }
        catch (Exception e){
            throw new RuntimeException("用户信息删除失败！");
        }
        return delete;
    }

    public List<SysUser> selectAll(){
        List<SysUser> sysUserList = null;
        try{
            sysUserList = sysUserDao.selectAll();
        }
        catch (Exception e){
            throw new RuntimeException("用户信息查询失败！");
        }
        return sysUserList;
    }

    public SysUser selectOne(int id){
        SysUser sysUser = null;
        try{
            sysUser = sysUserDao.selectOne(id);
        }
        catch (Exception e){
            throw new RuntimeException("用户信息查询失败！");
        }
        return sysUser;
    }
    // 手机端登陆使用
    public SysUser selectOne(String userName,String pwd){
        SysUser sysUser = null;
        try {
            pwd = MD5Utils.getMD5(pwd,32);
            sysUser = sysUserDao.login(userName,pwd);
        }
        catch (Exception e){
            // 将数据库操作Dao层的数据库异常，转换为Service的运行时异常
            throw new RuntimeException("用户不存在！【"+userName+"】");
        }
        return sysUser;

    }
    public List<SysUser> selectBy(String condition, int pageNum, int pageSize){
        List<SysUser> sysUserList = null;
        try{
            sysUserList = sysUserDao.selectBy(condition, pageNum, pageSize);
        }
        catch (Exception e){
            throw new RuntimeException("用户信息查询失败！");
        }
        return sysUserList;
    }

    public int countRows(String condition){
        int rows = 0;
        try{
            rows = sysUserDao.countRows(condition);
        }
        catch (Exception e){
            throw new RuntimeException("用户信息总笔数失败！");
        }
        return rows;
    }
}
